import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/dist/query/react";
export const newsApi = createApi({
  reducerPath: "newsApi",
  baseQuery: fetchBaseQuery({
    baseUrl: "/api",
  }),
  endpoints(build) {
    return {
      // 获取最新新闻  /news_latest  GET
      getNewsLatest: build.query({
        query() {
          return "/news_latest";
        },
      }),
      // 获取以往新闻  /news_before  GET
      getNewsBefore: build.query({
        query(time) {
          //这样拼接参数有些麻烦了，试试能不能封装
          return `/news_before/?time=${time}`;
        },
      }),
      // 获取新闻详细信息  /news_info  GET
      getNewsInfo: build.query({
        query(id) {
          return `/news_info/?id=${id}`;
        },
      }),
      // 获取新闻点赞信息  /story_extra  GET
      getStoryExtra: build.query({
        query(id) {
          return `/story_extra/?id=${id}`;
        },
      }),
      // 用户登录  /login  POST  @params phone:手机号码 code:短信验证码
      requestLogin: build.mutation({
        query(user) {
          return {
            url: "/login",
            method: "post",
            headers: {
              "Content-type": "application/x-www-form-urlencoded",
            },
            body: user,
          };
        },
      }),
      // 获取手机验证码  /phone_code  POST @paramsphone:手机号
      requestCode: build.mutation({
        query(phone) {
          return {
            url: "/phone_code",
            method: "post",
            headers: {
              "Content-type": "application/x-www-form-urlencoded",
            },
            body: phone,
          };
        },
      }),
    };
  },
});
// 导出自动生成的钩子函数。
export const {
  useGetNewsLatestQuery,
  useGetNewsBeforeQuery,
  useGetNewsInfoQuery,
  useGetStoryExtraQuery,
  useRequestLoginMutation,
  useRequestCodeMutation,
} = newsApi;

// personalApi
// 获取登录者信息  /user_info  GET
// 上传图片  /upload  POST
// 修改用户信息  /user_update  POST
// 收藏新闻  /store  POST
// // 移除收藏  /store_remove  GET

// loginApi
// 用户登录  /login  POS
// 获取手机验证码  /phone_code  POST
